import { updateStorage } from './tools'
import request from '../utils/request'
let userInfo = localStorage.getItem('userInfo')
try {
    userInfo = JSON.parse(userInfo) || {}
} catch (err) {
    userInfo = {}
}

export default {
    // 启用命名空间
    namespaced: true,
    state: {
        userInfo,
    },
    getters: {
        isLogin(state) {
            return !!state.userInfo.token
        }

    },
    mutations: {
        login(state, payload) {
            state.userInfo = payload
            updateStorage('userInfo', payload)
        },

    },
    actions: {
        async login(context, payload) {
            const { data } = await request.post('/login', payload)
            context.commit('login', data)
            return data
        },
        async regist(context, payload) {
            const { data } = await request.post('/reg', payload)
            return data
        }

    }
}